package com.mtiiot.fstcpserver.mq.kafka.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by Administrator on 2016/7/26.
 */
public class KafkaProperties {

    public static Properties properties;

    /**
     * 读取配置文件
     * */
    public static String config(String key) {
        String path = "kafkaP.properties";

        File file = new File(path);
        InputStream is = null;
        String content = null;
        try {
            is =  KafkaProperties.class.getClassLoader().getResourceAsStream(path);;
            Properties p = new Properties();
            p.load(is);
            content = p.getProperty(key);
        } catch (Exception e) {
            is = null;
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                is = null;
            }
        }
        return content;
    }

    // 加载配置文件
    public static Properties loadProperties() {
        Properties properties = null;
        String fileName = "kafkaP.properties";
        InputStream is = null;
        try {
            properties = new Properties();
            is =  KafkaProperties.class.getClassLoader().getResourceAsStream(fileName);;
            properties.load(is);
        } catch (Exception e) {
            is = null;
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                is = null;
            }
        }
        return properties;
    }

    // 根据文件路径加载配置文件
    public static Properties loadProperties(String filePath) {
        Properties properties = null;
        InputStream is = null;
        try {
            properties = new Properties();
            is = new FileInputStream(new File(filePath));
            properties.load(is);
        } catch (Exception e) {
            is = null;
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                is = null;
            }
        }
        return properties;
    }

    public static void main(String[] args) {
        System.out.println(config("metadata.broker.list"));
    }


}
